/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.TX;

import entity.Outlet;
import entity.OutletSeatArea;
import entity.Timeslot;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Lu Xiaodi
 */
@Stateless
public class TimeslotSB implements TimeslotBeanRemote {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext()
    EntityManager em;

    public String addTimeSlot(Long id, Long areaId, Integer time) {
        Outlet outlet = em.find(Outlet.class, id);
        if (outlet == null) {
            return "Outlet not found!";
        }

        OutletSeatArea area = em.find(OutletSeatArea.class, areaId);
        if (area == null) {
            return "area not found!";
        }
        if (!outlet.getSeatingAreas().contains(area)) {
            return "area not in outlet!";
        }
        //create new timeslot entity, containing quota for each area
        Timeslot timeslot = new Timeslot(time, area);

        String result = area.addTimeslot(timeslot);
        em.persist(area);
        em.flush();
        return result;
    }

    public List<Timeslot> getTimeSlots(Long id, Long areaId) { 
        final Outlet outlet = em.find(Outlet.class, id);
        if (outlet == null) {
            return null;
        }
        if (areaId == null) {
            return null;
        }
        OutletSeatArea area = em.find(OutletSeatArea.class, areaId);
        if (area == null) {
            return null;
        }
        if (!outlet.getSeatingAreas().contains(area)) {
            return null;
        }

        List<Timeslot> resultList = new ArrayList<Timeslot>();

        for (Timeslot t : area.getTimeslots()) {
            resultList.add(t);
        }
        return resultList;
    }

    public void saveTimeslot(Timeslot t) {
        Query query = em.createQuery("SELECT t FROM Timeslot t");
        List<Timeslot> tsList = query.getResultList();
        System.out.println(tsList);

        for (Timeslot t1 : tsList) {
            if (t1.getId().longValue() == t.getId().longValue()) {
                System.out.println("changing value in t1");
                System.out.println(t.getQuotaMon());
                t1.setQuotaMon(t.getQuotaMon());
                t1.setQuotaTue(t.getQuotaTue());
                t1.setQuotaWed(t.getQuotaWed());
                t1.setQuotaThu(t.getQuotaThu());
                t1.setQuotaFri(t.getQuotaFri());
                t1.setQuotaSat(t.getQuotaSat());
                t1.setQuotaSun(t.getQuotaSun());
                em.persist(t1);
            }
        }
    }

    public String removeTimeSlot(Long id, Long areaId, Integer time) {
        Outlet outlet = em.find(Outlet.class, id);
        if (outlet == null) {
            return "Outlet not found!";
        }
        OutletSeatArea area = em.find(OutletSeatArea.class, areaId);
        if (area == null) {
            return "area not found!";
        }
        if (!outlet.getSeatingAreas().contains(area)) {
            return "area not in outlet!";
        }

        String resultStr = "";
        List<Timeslot> timeslotList = area.getTimeslots();
        for (int i = 0 ; i < timeslotList.size(); i++) {
            System.out.print(i);
            Timeslot ts =  timeslotList.get(i);
            if (ts.getTsTime().intValue() == time.intValue()) {
                area.deleteTimeslot(ts);
                em.remove(ts);
                resultStr = "timeslot successfully removed";
            }
        }
        em.persist(outlet);
        em.persist(area);
        em.flush();
        return resultStr;
    }
}
